package com.ohimy.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.ohimy.dao.impl.ForumBodyDao;
import com.ohimy.dao.impl.ForumDao;
import com.ohimy.dao.impl.GroupDao;
import com.ohimy.entity.Forum;
import com.ohimy.entity.ForumBody;

@Service("forumService")
public class ForumService {

	@Resource(name = "forumDao")
	private ForumDao forumDao;

	@Resource(name = "forumBodyDao")
	private ForumBodyDao forumBodyDao;

	@Resource(name = "groupDao")
	private GroupDao groupDao;

	/**
	 * 得到所有该群组的论坛主题总数
	 * 
	 * @param id
	 * @return
	 */
	public long getForumByTotal(long id) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("group",
				groupDao.get(id)) };
		return forumDao.findByCriteria(criterions).size();
	}

	/**
	 * 论坛主题的分页
	 * 
	 * @param id
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	public List<Forum> getForumByPager(long id, int firstResult, int maxResult) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("group",
				groupDao.get(id)) };
		return forumDao.findByCriteria(firstResult, maxResult,
				Order.desc("lastPublish"), criterions);
	}

	/**
	 * 得到所有该群组的论坛回复总数
	 * 
	 * @param id
	 * @return
	 */
	public long getForumBodyByTotal(long id) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("forum",
				forumDao.get(id)) };
		return forumBodyDao.findByCriteria(criterions).size();
	}

	/**
	 * 论坛主题回复的分页
	 * 
	 * @param id
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	public List<ForumBody> getForumBodyByPager(long id, int firstResult,
			int maxResult) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("forum",
				forumDao.get(id)) };
		return forumBodyDao.findByCriteria(firstResult, maxResult, null,
				criterions);
	}

	/**
	 * 保存一个帖子
	 * 
	 * @param forum
	 */
	public void save(Forum forum) {
		forumDao.save(forum);
	}
	
	/**
	 * 更新最后一次回复
	 * @param forum
	 */
	public void updateLastPublish(Forum forum) {
		forumDao.update(forum);
	}

	/**
	 * 发表一个回复,返回刚保存的回复
	 * 
	 * @param forumBody
	 */
	public List<ForumBody> saveReply(ForumBody forumBody) {
		List<ForumBody> result = new ArrayList<ForumBody>();
		long reId = forumBodyDao.save(forumBody);
		result.add(forumBodyDao.get(reId));
		return result;
	}

	/**
	 * 更新帖子查看次数，并返回最新实体
	 * 
	 * @param id
	 */
	public Forum updateViewNumber(long id) {
		Forum forum = getForumById(id);
		long viewTime = forum.getViewTime();
		forum.setViewTime(++viewTime);
		return forumDao.update(forum);
	}

	/**
	 * 通过Id得到论坛实体
	 * 
	 * @return
	 */
	public Forum getForumById(long id) {
		return forumDao.get(id);
	}

	/* ****************************** */

	public void setForumDao(ForumDao forumDao) {
		this.forumDao = forumDao;
	}

	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}

	public void setForumBodyDao(ForumBodyDao forumBodyDao) {
		this.forumBodyDao = forumBodyDao;
	}

}
